import { createRouter, createWebHashHistory } from 'vue-router'
import UserLogin from '@/components/UserLogin.vue'
import UserRegister from '@/components/UserRegister.vue'
import UserForget from '@/components/UserForget.vue'
import Home from '@/components/home.vue'
import welcome from '@/components/welcome.vue'
import Users from '@/components/user/User.vue'
import Rights from '@/components/power/Rights.vue'
import Roles from '@/components/power/Roles.vue'
import Cate from '@/components/goods/Cate.vue'
import Params from '@/components/goods/Params.vue'
import Products from '@/components/goods/Products.vue'
import Orders from '@/components/order/Order.vue'
import Report from '@/components/report/Report.vue'
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: UserLogin },
  { path: '/register', component: UserRegister },
  { path: '/forget', component: UserForget },
  { path: '/home', component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: welcome },
      { path: '/user/list/', component: Users },
      { path: '/api/permissions/', component: Rights },
      { path: '/api/roles/', component: Roles },
      { path: '/api/categories/',component: Cate },
      { path: '/api/category_params/',component: Params },
      { path: '/api/products/',component: Products },
      { path: '/api/orders/',component: Orders },
      { path: '/api/statistics/',component: Report },
    ]
   }
]



const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//挂载路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register' || to.path === '/forget') {
    next();
  } else {
    const token = sessionStorage.getItem('token');
    if (token) {
      next();
    } else {
      next('/login');
    }
  }
})
export default router
